<?php
/*
 * File LogObserverMail.php for project Rijlesplanner.nl
 * Created on 19 nov 2007 at 20:40:32 by Ivar Pruijn
 */

require_once PEAR_DIR.'Log/observer.php';

class LogObserverMail extends Log_observer {
   	private $smtp_host = '';
    private $smtp_port = 25;
    private $sender_email = '';
    private $sender_description = '';
    private $_subject = '';
    private $_pattern = '';

    function Log_observer_mail($priority, $conf) {
        /* Call the base class constructor. */
        $this->Log_observer($priority);

        /* Configure the observer. */
        $this->smtp_host = $conf['smtp_host'];
        $this->smtp_port = $conf['smtp_port'];
        $this->sender_email = $conf['$sender_email'];
        $this->sender_description = $conf['$sender_description'];
        $this->_subject = $conf['subject'];
        $this->_pattern = $conf['pattern'];
    }

    function notify($event)
    {
        if (preg_match($this->_pattern, $event['message']) != 0) {
			try {
				//Start Swift
				$swift =& new Swift(new Swift_Connection_SMTP($this->smtp_host,$this->smtp_port));
			
				$mailbody = $event['message'];
				// Put headers and footers to the body
				//Create the message, using some unique variables to search for
				$message =& new Swift_Message($this->_subject, $mailbody, "text/html");
				if(!$this->_swift->send($message, new Swift_Address($this->receiver_email, $this->receiver_description), new Swift_Address($this->sender_email, $this->sender_description))) {
					// didn't go okay
					throw new RijlesplannerException('Error while sending logging e-mail');
				}
			} catch (Swift_ConnectionException $e) {
			  echo "There was a problem communicating with SMTP: " . $e->getMessage();
			  return false;
			} catch (Swift_Message_MimeException $e) {
			  echo "There was an unexpected problem building the email:" . $e->getMessage();
			  return false;
			} catch (Swift_ConnectionException $e) {
			  echo "There was a problem communicating with the SMTP server: " . $e->getMessage();
			  return false;
			}
        }
    }
}

?>
